package com.icat.house.common;

import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * 日期工具类
 * 
 * @description
 * @author lixinlin
 * @date 2017年1月9日 上午9:55:06
 *
 */
public class DateUtil {

	public static final String YYYYMMDD = "yyyyMMdd";
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String CN_YYYY_MM_DD = "yyyy年MM月dd日";
	public static final String HH_MM_SS = "HH:mm:ss";
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final String MM_DD_HH_MM_SS = "MM-dd HH:mm:ss";
	public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
	public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";
	// 本工具类默认日期格式
	public static final String DEFAULT_DATE_FORMAT = YYYY_MM_DD;

	public static String addDay(String data) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cd = Calendar.getInstance();
		try {
			cd.setTime(sdf.parse(data));
			cd.add(5, 1);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return sdf.format(cd.getTime());
	}

	/**
	 * 传入相隔天数，返回相隔日期
	 * 
	 * @param num
	 * @return
	 * @throws Exception
	 */
	public static Date date(int num) throws Exception {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -num);
		Date date = c.getTime();
		return date;
	}

	public static String addDay(int add) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cd = Calendar.getInstance();
		cd.setTime(new Date());
		cd.add(5, add);
		return sdf.format(cd.getTime());
	}

	public static String addDay(String data, int add) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cd = Calendar.getInstance();
		try {
			cd.setTime(sdf.parse(data));
			cd.add(5, add);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return sdf.format(cd.getTime());
	}

	public static String getNow() {

		return getDate(new Date());
	}

	public static String getDate(Date date) {
		return format(date, "yyyy-MM-dd");
	}

	/**
	 * 字符串转日期：指定格式
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date parse(String dateStr, String format) {
		Date date = null;
		try {
			SimpleDateFormat dateFormat = new SimpleDateFormat(format);
			date = dateFormat.parse(dateStr);
		} catch (Exception localException) {
			// Exception return null
		}
		return date;
	}

	/**
	 * 获取几天后的当前时间
	 * 
	 * @param add
	 * @return
	 */
	public static Date getDate(int add) {
		Date date = null;
		Calendar now = Calendar.getInstance();
		// 得到当前时间，+7天
		now.add(Calendar.DAY_OF_MONTH, add);
		// 进行时间转换
		date = now.getTime();
		return date;
	}

	/**
	 * 传入相隔天数，返回相隔日期
	 * 
	 * @param num
	 * @return
	 * @throws Exception
	 */
	public static String date(String dateStr, int num) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance();
		c.setTime(sdf.parse(dateStr));
		c.add(Calendar.DATE, -num);
		Date date = c.getTime();
		String start = sdf.format(date);
		return start;
	}

	/**
	 * 字符串转日期：默认格式
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parse(String dateStr) {
		return parse(dateStr, DEFAULT_DATE_FORMAT);
	}

	/**
	 * 日期转字符串：指定格式
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String format(Date date, String format) {
		String result = "";
		try {
			if (date != null) {
				DateFormat dateFormat = new SimpleDateFormat(format);
				result = dateFormat.format(date);
			}
		} catch (Exception localException) {
		}
		return result;
	}

	/**
	 * 日期转字符串：默认格式
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		String result = "";
		try {
			if (date != null) {
				DateFormat dateFormat = new SimpleDateFormat(YYYYMMDD);
				result = dateFormat.format(date);
			}
		} catch (Exception localException) {
		}
		return result;
	}

	/**
	 * 日期转字符串：默认格式
	 * 
	 * @param date
	 * @return
	 */
	public static String format(Date date) {
		String result = "";
		try {
			if (date != null) {
				DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
				result = dateFormat.format(date);
			}
		} catch (Exception localException) {
		}
		return result;
	}

	/**
	 * 日期转字符串：特定格式
	 * 
	 * @param date
	 * @return
	 */
	public static String specialFormat(Date date) {
		String result = "";
		try {
			if (date != null) {
				DateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM);
				result = dateFormat.format(date);
			}
		} catch (Exception localException) {
		}
		return result;
	}

	/**
	 * 通过网络获取北京时间
	 * 
	 * @return
	 */
	public static String getServiceTime() {
		String result = "";
		SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		try {
			TimeZone.setDefault(TimeZone.getTimeZone("GMT+8")); // 时区设置
			URL url = new URL("http://www.beijing-time.org");// 取得资源对象
			URLConnection uc = null;
			uc = url.openConnection();// 生成连接对象
			uc.connect(); // 发出连接
			long ld = uc.getDate(); // 取得网站日期时间（时间戳）
			Date date = new Date(ld); // 转换为标准时间对象
			result = format.format(date);
		} catch (Exception localException) {
		}
		return result;
	}

	/**
	 * 两个日期的相差天数(date2-date1)
	 * 
	 * @param date2
	 * @param date1
	 * @return
	 */
	public static int daysDiff(Date date2, Date date1) {
		if (date2 == null || date1 == null) {
			throw new IllegalArgumentException();
		}
		return new Float((date2.getTime() - date1.getTime()) / 86400000L).intValue();
	}

	/**
	 * 判断是否为周末
	 * 
	 * @param dateInt
	 * @return
	 * @throws Exception
	 */
	public static boolean isholiday(String dateStr) throws Exception {
		boolean result = false;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date date = sdf.parse(String.valueOf(dateStr));
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			result = true;
		}
		return result;
	}

	/**
	 * 获取两个日期之间日期的List
	 *
	 * @param start
	 * @param end
	 * @return
	 * @throws Exception
	 */
	public static List<Date> dateSplit(String start, String end) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date startDate = sdf.parse(start);
		Date endDate = sdf.parse(end);
		if (!startDate.before(endDate))
			throw new Exception("After the start time should be at the end of time");
		Long spi = endDate.getTime() - startDate.getTime();
		Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数

		List<Date> dateList = new ArrayList<Date>();
		dateList.add(endDate);
		for (int i = 1; i <= step; i++) {
			dateList.add(new Date(dateList.get(i - 1).getTime() - (24 * 60 * 60 * 1000)));// 比上一天减一
		}

		List<Date> dates = new ArrayList<Date>();
		if (!dateList.isEmpty()) {
			for (int i = 0; i < dateList.size(); i++) {
				Date date = dateList.get(i);
				Calendar cal = Calendar.getInstance();
				cal.setTime(date);
				dates.add(date);
			}
		}
		return dates;
	}

	/**
	 * 两个日期(默认格式)的相差天数(date2-date1)，
	 * 
	 * @param date2Str
	 * @param date1Str
	 * @return
	 */
	public static int daysDiff(String date2Str, String date1Str) {
		return daysDiff(parse(date2Str), parse(date1Str));
	}

	public static int getYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(1);
	}

	public static int getMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(2) + 1;
	}

	public static int getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(5);
	}

	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(11);
	}

	public static int getMinute(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(12);
	}

	public static int getSecond(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(13);
	}

	public static long getMillis(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getTimeInMillis();
	}

	public static Date addDate(Date date, int day) {
		Calendar calendar = Calendar.getInstance();
		long millis = getMillis(date) + day * 24L * 3600L * 1000L;
		calendar.setTimeInMillis(millis);
		return calendar.getTime();
	}

	public static String stampToDate(long time) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm");
		Date date = new Date(time);
		return simpleDateFormat.format(date);
	}

}
